package com.dream.keigezhushou.Activity.acty.personal;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.activity.BaseActivity;
import com.dream.keigezhushou.Activity.acty.personal.fragment.AlreadyUseFragment;
import com.dream.keigezhushou.Activity.acty.personal.fragment.NotUsedFragment;
import com.dream.keigezhushou.Activity.acty.personal.fragment.OverdueFragment;
import com.dream.keigezhushou.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DiscountActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.iv_return)
    ImageView ivReturn;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_no_use)
    TextView tvNoUse;
    @BindView(R.id.tv_used)
    TextView tvUsed;
    @BindView(R.id.tv_overdue)
    TextView tvOverdue;
    @BindView(R.id.vp_contain)
    ViewPager vpContain;
    private ArrayList<Fragment> list;
    private FragmentPagerAdapter adapter;

    private int currIndex;//当前页卡编号

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discount);
        ButterKnife.bind(this);

        initView();
        initData();
        initEvent();
    }

    //初始化界面
    protected void initView(){

        tvTitle.setText("优惠券");
    }

    //初始化数据
    protected void initData(){

        //创建fragment集合
        list = new ArrayList<Fragment>();
        //集合添加元素
        list.add(new NotUsedFragment());
        list.add(new AlreadyUseFragment());
        list.add(new OverdueFragment());

        //创建适配器
        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        };

        vpContain.setAdapter(adapter);

        //默认选中第一个
        tvNoUse.setSelected(true);

    }

    //初始化事件
    protected void initEvent(){

        ivReturn.setOnClickListener(this);
        tvNoUse.setOnClickListener(this);
        tvUsed.setOnClickListener(this);
        tvOverdue.setOnClickListener(this);

        //vp滑动改变的监听
        vpContain.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                updateTopIcon(position);
                vpContain.setCurrentItem(position);
                int i = currIndex + 1;

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void updateTopIcon(int position){

        if (position == 0){

            tvNoUse.setSelected(true);
            tvUsed.setSelected(false);
            tvOverdue.setSelected(false);
        }

        if (position == 1){

            tvNoUse.setSelected(false);
            tvUsed.setSelected(true);
            tvOverdue.setSelected(false);
        }

        if (position == 2){

            tvNoUse.setSelected(false);
            tvUsed.setSelected(false);
            tvOverdue.setSelected(true);
        }
    }

    //点击事件的实现

    @Override
    public void onClick(View view) {

        switch (view.getId()){

            case R.id.iv_return:

                //返回
                finish();
                break;

            case R.id.tv_no_use:

                tvNoUse.setSelected(true);
                tvUsed.setSelected(false);
                tvOverdue.setSelected(false);
                vpContain.setCurrentItem(0);
                break;

            case R.id.tv_used:

                tvNoUse.setSelected(false);
                tvUsed.setSelected(true);
                tvOverdue.setSelected(false);
                vpContain.setCurrentItem(1);
                break;
            case R.id.tv_overdue:

                tvNoUse.setSelected(false);
                tvUsed.setSelected(false);
                tvOverdue.setSelected(true);
                vpContain.setCurrentItem(2);
                break;

        }

    }
}
